/*!
 * 平台脚本常用函数
 * 主要用于格式化日期等
 * @author 吴晓峰
 * @date 2014-12- 22
 */

var miceCommonFunction = {
    /**
     * 格式化日期
     * 返回 年-月-日 格式
     */
    jsonDateFormat: function (jsonDate) {
        try {
            var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            return date.getFullYear() + "-" + month + "-" + day;
        } catch (ex) {
            return "";
        }
    },
    /**
     * 格式化日期
     * 返回 年-月-日-时-分-秒 格式
     */
    jsonDateTimeFormat: function (jsonDate) {
        try {
            var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        } catch (ex) {
            return "";
        }
    },
    getEnumList: function (type, successcallbackAction) {
        sendRequest("GetGetEnumDescriptions", "POST", { type: type }, successcallbackAction);
    },
    baseUrl: "",
    request : function(strParame){
        var args = new Object( );
        var query = location.search.substring(1);

        var pairs = query.split("&"); // Break at ampersand
        for(var i = 0; i < pairs.length; i++) {
            var pos = pairs[i].indexOf('=');
            if (pos == -1) continue;
            var argname = pairs[i].substring(0,pos);
            var value = pairs[i].substring(pos+1);
            value = decodeURIComponent(value);
            args[argname] = value;
        }
        return args[strParame];
    },
    reject: function (inquiryid) {
        var url = "";
        if (miceCommonFunction.baseUrl.indexOf('Inquiry') < 0) {
            url += miceCommonFunction.baseUrl + "Inquiry/";
        }
        url += 'Reject?inquiryID=' + inquiryid;
        art.dialog.open(url,
            {
                width: '350px',
                height: '230px',
                lock: true,
                title: '温馨提示',
            });
    },
    /*四舍五入两位小数*/
    changeTwoDecimal: function (x)
    {
        var f_x = parseFloat(x);
        if (isNaN(f_x))
        {
            alert('请输入的数值');
            return 0;
        }
        var f_x = Math.round(x*100)/100;

        return f_x;
    },
    /*对象Coloe*/
    clone: function (sObj) {
        if (typeof sObj !== "object") {
            return sObj;
        }
        var s = {};
        if (sObj.constructor == Array) {
            s = [];
        }
        for (var i in sObj) {
            s[i] = miceCommonFunction.clone(sObj[i]);
        }
        return s;
    },
    limitLength: function (o) {
        var len = jQuery(o).attr("maxlength");
        if (jQuery(o).val().trim().length > len) {
            alert("长度不能大于" + len + "，请重新输入！");
            jQuery(o).focus();
            return;
        }
    }
};
var ValueSpanViewModel = function (fromVal, toVal) {
    var self = this;
    this.FromValue = ko.observable(fromVal);
    this.ToValue = ko.observable(toVal);
    this.FromValue.subscribe(
        function(newVal) {
            if (newVal == "") {
                self.FromValue(null);
                //alert(self.FromValue());
            }
        }
    );
    this.ToValue.subscribe(
        function (newVal) {
            if (newVal == "") {
                self.ToValue(null);
            }
        }
    );

};

var enumTypeModel = {
    RequireDetailEnumType: 10,//需求单详情状态
    TravelEnumType: 11,//行程类型
    InquiryEnumType: 12,//询价单状态
    OrderEnumType: 13//订单状态
};
/**
 *出游性质枚举
 **/
var TravelNatureEnumType = {
    ALL: ko.observable({ key: 0, val: "未定义" }),
    Company: ko.observable({ key: 10, val: "公司包团" }),
    Confernce: ko.observable({ key: 20, val: "会务会展" }),
    BigCustomer: ko.observable({ key: 30, val: "大客户" }),
    Customize: ko.observable({ key: 40, val: "大客户" })
};

/**
 * Tab对象类
 *@title Tab标题
 *@currentCss Tab当前样式
 *@status 对应的数据状态
 *@loadDataAction 数据加载函数
 */
var TabModel = function (title, currentCss, status, loadDataAction) {
    var self = this;
    self.Title = ko.observable(title);
    self.CurrentCss = ko.observable(currentCss);
    self.Status =ko.observable(status);
    self.LoadDataAction = loadDataAction;
    self.Index = ko.observable(0);
    self.ID = ko.observable(0);
    self.Remark = ko.observable("");
};
/**
 * 区域对象类
 *@id ID号
 *@name 区域名称
 *@isChecked 是否选中
 *@isChecked 选中后的事件
 */
var AreaModel = function (id, name, isChecked, ckAction) {
    var self = this;
    self.ID = ko.observable(id);
    self.Name = ko.observable(name);
    self.IsChecked = ko.observable(isChecked);
    self.IsChecked.subscribe(function (newVal) {
        if (ckAction != null && ckAction != 'undefined') {
            ckAction(self);
        }
    });
};

var ActionItemViewModel = function (title, event, isVisible) {
    var self = this;
    self.Title = title;
    self.Event = event;
    self.isVisible = isVisible;
};

function SubString(str, len) {
    var result = "";
    str = str + '';
    var count = 0;
    for (var i = 0; i < len; i++) {
        var _char = str.charAt(i);
        if (count >= len) break;
        if (/[^x00-xff]/.test(_char)) {
            count++;
        }
        result += _char;
        count++;
    }
    if (result.length < str.length) {
        result += "...";
    }
    return result;
};